<template>
  <div>
    <el-card
      shadow="never"
      style="border: none"
    >
      <div slot="header">
        <div class="header_title">
          <span>处理列表</span>
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >新增</el-button>
        </div>
      </div>

      <el-table
        v-loading="loading"
        :data="itemList"
      >
        <el-table-column
          label="更新时间"
          align="center"
          prop="updateTime"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="整改措施"
          align="center"
          prop="modifiedMethod"
        />
        <el-table-column
          label="描述"
          align="center"
          prop="description"
        />
        <el-table-column
          label="备注"
          align="center"
          prop="remark"
        />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:item:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:item:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNo"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item
          label="整改措施"
          prop="modifiedMethod"
        >
          <el-input
            v-model="form.modifiedMethod"
            placeholder="请输入整改措施"
          />
        </el-form-item>
        <el-form-item label="处理情况" prop="status">
          <el-select v-model="form.status" placeholder="请选择当前状态">
            <el-option
              v-for="dict in dict.type.sys_task_handle_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="描述"
          prop="description"
        >
          <el-input
            v-model="form.description"
            placeholder="请输入描述"
          />
        </el-form-item>
        <el-form-item
          label="备注"
          prop="remark"
        >
          <el-input
            v-model="form.remark"
            placeholder="请输入备注"
          />
        </el-form-item>
        <el-form-item label="附件">
          <el-upload
            action="#"
            list-type="picture-card"
            multiple
            :file-list="form.fileList"
            :auto-upload="false"
            :on-change="handleImage"
            :on-remove="removeImage"
            :on-preview="handlePictureCardPreview"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog
            :visible.sync="dialogVisible"
            append-to-body
          >
            <img
              width="100%"
              :src="dialogImageUrl"
              alt=""
            >
          </el-dialog>
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          type="primary"
          @click="submitForm"
        >确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  listItem,
  getItem,
  delItem,
  addItem,
  updateItem,
} from "@/api/accident/item";
import { uploadFile, deleteFiles, deleteFile } from "@/api/system/file";
export default {
  name: "AccidentItems",
  dicts: ['sys_task_handle_type'],
  props: {
    accId: {
      type: String,
      required: true,
    },
    flush: {
      type: Number,
      default: 0,
    },
  },
  watch: {
    flush(newVal) {
      debugger
      this.getList();
    }
  },
  data() {
    return {
      total: 0,
      // 表格数据
      itemList: [],
      title: "",
      // 是否显示弹出层
      open: false,
      dialogImageUrl: "",
      dialogVisible: false,
      form: {},
      // 表单校验
      rules: {},
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        accId: null,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleImage(file, fileList) {
      this.form.fileList = fileList;
    },
    removeImage(file, fileList) {
      deleteFile(file.id).then((res) => {

      })
      console.log(file);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },

    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      this.queryParams.accId = this.accId;
      listItem(this.queryParams).then((response) => {
        this.itemList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        accId: this.accId,
        status: null,
        description: null,
        remark: null,
        type: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        modifyBy: null,
        modifiedMethod: null,
        taskId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加处理记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getItem(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改处理";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      var that = this;
      if (this.form.id != null) {
        updateItem(this.form).then((response) => {
          this.$modal.msgSuccess("修改成功");
          this.open = false;
          this.getList();
          const files = this.form.fileList
            .filter((item) => !!item.raw)
            .map((item) => item.raw);
          if (files.length > 0) {
              const fd = new FormData();
              fd.append("connectId", that.form.id);
              fd.append("busiType", 2);
              files.forEach((file) => {
                fd.append("file", file);
              });
              uploadFile(fd).then(()=>{
              this.$emit('data-flush')
            });
            }else{
              this.$emit('data-flush')
            }
        });
      } else {
        addItem(this.form).then((response) => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          let temp = response.data;
          this.form.id = temp.id;
          this.getList();
          const files = this.form.fileList
            .filter((item) => !!item.raw)
            .map((item) => item.raw);
          if (files.length > 0) {
            const fd = new FormData();
            fd.append("connectId", this.form.id);
            fd.append("busiType", 2);
            files.forEach((file) => {
              fd.append("file", file);
            });
            uploadFile(fd).then(()=>{
              this.$emit('data-flush')
            });
          }else{
              this.$emit('data-flush')
            }
        });
      }

    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      var that = this;
      this.$modal
        .confirm('是否确认删除处理记录编号为"' + ids + '"的数据项？')
        .then(function () {
          delItem(ids).then(() =>{
            that.getList();
            that.$emit('data-flush');
          })
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
<style lang="scss" scoped>
.header_title {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}
</style>
